package com.easygo.controller;

import com.easygo.api.BrandClient;
import com.easygo.pojo.Brand;
import com.easygo.utils.MessageResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: easygo
 * @BelongsPackage: com.easygo.controller
 * @Author: wangzhen
 * @QQ:425042686
 * @CreateTime: 2024-03-27 11:24
 * @Description: TODO
 */
@Controller
@Scope("prototype")
public class BrandController {

    // 注入BrandClient
    @Autowired
    BrandClient brandClient;

    // 获取品牌列表
    @RequestMapping("/brand_getBrands")
    public String getBrands(Model model) {
        // 调用BrandClient的getBrands方法获取品牌列表
        List<Brand> brands = brandClient.getBrands();
        // 将品牌列表添加到model中
        model.addAttribute("brands", brands);
        // 返回brand页面
        return "brand";
    }

    // 添加品牌
    @RequestMapping("/brand_add")
    @ResponseBody
    public MessageResults addBrand(@RequestBody Brand brand) {
        // 创建MessageResults对象
        MessageResults results = null;
        // 调用BrandClient的addBrand方法添加品牌
        int count = brandClient.addBrand(brand);
        // 判断添加是否成功
        if (count > 0) {
            // 添加成功
            results = new MessageResults(200, "新增成功");
        } else {
            // 添加失败
            results = new MessageResults(500, "新增失败");
        }
        // 返回MessageResults对象
        return results;
    }

    // 根据id获取品牌
    @RequestMapping("/brand_getBrandById")
    @ResponseBody
    public Brand getBrandById(Integer id){
        // 调用BrandClient的getBrandById方法根据id获取品牌
        return brandClient.getBrandById(id);
    }

    // 更新品牌
    @RequestMapping("/brand_update")
    @ResponseBody
    public MessageResults brand_update(Brand brand){
        // 创建MessageResults对象
        MessageResults results = null;
        // 打印要更新的对象
        System.out.println("要更新的对象是："+brand);
        // 调用BrandClient的updateBrand方法更新品牌
        int count = brandClient.updateBrand(brand);
        // 判断更新是否成功
        if (count > 0) {
            // 更新成功
            results = new MessageResults(200, "更新成功");
        } else {
            // 更新失败
            results = new MessageResults(500, "更新失败");
        }
        // 返回MessageResults对象
        return results;
    }

    // 删除品牌
    @RequestMapping("/brand_delete")
    @ResponseBody
    public MessageResults delete(Integer id){
        // 创建MessageResults对象
        MessageResults results = null;
        // 调用BrandClient的deleteBrand方法删除品牌
        int count = brandClient.deleteBrand(id);
        // 判断删除是否成功
        if (count > 0) {
            // 删除成功
            results = new MessageResults(200, "删除成功");
        } else {
            // 删除失败
            results = new MessageResults(500, "删除失败");
        }
        // 返回MessageResults对象
        return results;
    }

    // 批量删除品牌
    @RequestMapping("/brand_deleteSome")
    @ResponseBody
    public MessageResults deleteSome(String ids){
        // 创建MessageResults对象
        MessageResults results = null;
        //ids=11,12,13
        System.out.println("ids="+ ids);
        int count = brandClient.deleteSome(ids);
        if (count > 0) {
            results = new MessageResults(200, "批量删除成功");
        } else {
            results = new MessageResults(500, "批量删除失败");
        }
        return results;
    }
}
